package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig{

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);//第一种写法：单参构造方法，默认就开启了持久化
        //return new DirectExchange(DIRECT_EXCHANGE,true,false); //第二种写法：显示指定持久化
        //return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();//第三种写法：显示指定持久化
    }

    @Bean
    public Queue directQueue(){
        //return new Queue(DIRECT_QUEUE); //第一种写法：单参数构造方法：默认就是开启持久化
        //return new Queue(DIRECT_QUEUE,true); //第二种写法：显示指定持久化
        return QueueBuilder.durable(DIRECT_QUEUE).build(); //第三种写法：显示指定持久化
    }

    @Bean
    public Binding bindDirectQueue(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }
}
